package com.sghd.football.module.game.service.core;

import com.sghd.football.module.game.service.model.Result;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * \* Created with 王创辉.
 * \* Date: 2018/5/18
 * \* Time: 14:40
 * \* @desc:棋盘上面的格子
 */
public class Grid{

    private ChessType chess;

    private transient List<Integer> eqs = new ArrayList<>();

    public static Grid valueOf(ChessType chess){
        Grid grid = new Grid();
        grid.chess = chess;
        return grid;
    }

    public void added(int idx){
        eqs.add(idx);
    }

    public void checkResult(int lineIdx, Result result, Grid[] grids){
        if(eqs.size() >= chess.getLimit()){//有线
            Set<ChessType> distinct = new HashSet<>();//去重
            for(int idx :eqs){
                distinct.add(grids[idx].getChess());
            }
            if(distinct.size() != 1 || !distinct.contains(ChessType.CT_WILD)){
                result.addLine(lineIdx, eqs.toArray(new Integer[eqs.size()]));
            }
        }
        eqs.clear();
    }
    public void update(ChessType chess){
        eqs.clear();
        this.chess = chess;
    }
    public ChessType getChess() {
        return chess;
    }
    public void setChess(ChessType chess) {
        this.chess = chess;
    }

    public boolean equals(Grid next) {
        if(next.chess == chess){
            return true;
        }
        if(next.chess.contains(this.chess)){
            return true;
        }
        if(this.chess.contains(next.chess)){
            return true;
        }
        return false;
    }

    public void clean() {
        eqs.clear();
    }
}
